﻿using System.Text;
using VideoTools.SubtitleEdit.BluRaySup;
using VideoTools.SubtitleEdit.Common;
using static VideoTools.SubtitleEdit.BluRaySup.BluRaySupParser;

namespace VideoTools.service
{
    /// <summary>
    /// sup字幕解析
    /// </summary>
    internal class SupImageSubtitleService : IImageSubtitleService
    {
        private readonly string filePath;
        private StringBuilder log;
        //sup
        private List<PcsData> pcsList;

        public SupImageSubtitleService(string filePath)
        {
            this.filePath = filePath;
            log = new StringBuilder();
        }

        private Bitmap GetSubtitleBitmap(int index)
        {
            Bitmap returnBmp = null;
            List<PcsData> tempList = pcsList;
            if (tempList != null)
            {
                if (tempList != null)
                {
                    if (index >= 0 && index < tempList.Count)
                    {
                        returnBmp = tempList[index].GetBitmap();
                    }
                }
            }
            return returnBmp;
        }
        public Bitmap subtitleBitmap(int index)
        {
            int numberOfImages = GetSubtitleCount();
            Bitmap bmp = null;
            if (index < numberOfImages)
            {
                bmp = GetSubtitleBitmap(index);
                if (bmp == null)
                {
                    bmp = new Bitmap(1, 1);
                }
                return bmp;
            }
            return new Bitmap(1, 1);
        }

        public SubtitleEncode parseSubtitle()
        {
            pcsList = BluRaySupParser.ParseBluRaySup(filePath, log);
            SubtitleEncode _subtitle = null;
            if (pcsList.Count == 0)
            {
                return _subtitle;
            }
            if (pcsList != null)
            {
                _subtitle = new SubtitleEncode();
                _subtitle.FileName = filePath;
                int max = pcsList.Count;
                for (int i = 0; i < max; i++)
                {
                    var x = pcsList[i];
                    _subtitle.Paragraphs.Add(new Paragraph
                    {
                        StartTime = new TimeCode(x.StartTime / 90.0),
                        EndTime = new TimeCode(x.EndTime / 90.0)
                    });
                }
            }
            return _subtitle;
        }


        public int GetSubtitleCount()
        {
         
            return pcsList.Count;
        }
    }
}
